﻿#include "certificatecenter.h"
#include "ui_certificatecenter.h"
#include <QMessageBox>

CertificateCenter::CertificateCenter(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CertificateCenter)
{
    ui->setupUi(this);

    m_hotelDetails = nullptr;
    m_hotelModel = new HotelCertModel;
    ui->tabv_hotelCert->setModel(m_hotelModel);
    ui->tabv_hotelCert->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    ui->tabv_hotelCert->horizontalHeader()->setStretchLastSection(true);
    ui->tabv_hotelCert->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    ui->tabWidget->setCurrentIndex(0);
}

CertificateCenter::~CertificateCenter()
{
    delete ui;
}

int CertificateCenter::getCurrentIndex()
{
    return ui->tabWidget->currentIndex();
}

void CertificateCenter::setCurentIndex(int index)
{
    ui->tabWidget->setCurrentIndex(index);
}

void CertificateCenter::slotRefreshHotelCertList()
{
    ui->lb_hotelCertNum->setText(QString::number(GlobalVars::g_hotelManageList->size()));
    m_hotelModel->refrushModel();
}

void CertificateCenter::on_tabv_hotelCert_doubleClicked(const QModelIndex &index)
{
    emit signalGainHotelDetails(m_hotelModel->getHotelID(index.row()));
}

void CertificateCenter::setHotelDetails()
{
    if (m_hotelDetails != nullptr)
    {
        delete m_hotelDetails;
    }
    m_hotelDetails = new RegisterHotel();
    m_hotelDetails->setHotelInfo();
    m_hotelDetails->closePushButton();
    m_hotelDetails->setWindowTitle("酒店详情");
    m_hotelDetails->setWindowModality(Qt::ApplicationModal);
    m_hotelDetails->setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint); // 禁止最大化按钮
    m_hotelDetails->setFixedSize(m_hotelDetails->width(),m_hotelDetails->height());   // 禁止拖动窗口大小
    m_hotelDetails->show();

    connect(m_hotelDetails,SIGNAL(signalHotelPassCert(bool,const QString&)),
            this, SIGNAL(signalHotelPassCert(bool,const QString&)));
}

void CertificateCenter::slotHotelImage()
{
    m_hotelDetails->setImages();
}

void CertificateCenter::slotHotelCertResult(bool result, const QString& msg)
{
    if (result)
    {
        m_hotelDetails->close();
        emit signalRefrushCertList(getCurrentIndex());      /// 重新加载
    }
    else
    {
        m_hotelDetails->errorShow(msg);
    }
}

void CertificateCenter::slotUserCertList(bool result, const QString& msg)
{
    if (result)
    {
        ui->lb_IDCertNum->setText(QString::number(GlobalVars::g_userInfoList->size()));
        CertContent *certContent = new CertContent();
        m_idCertVector.append(certContent);
        ui->lwg_IDCert->addItem("");
        ui->lwg_IDCert->setItemWidget(ui->lwg_IDCert->item(m_idCertVector.size()-1),certContent);
        ui->lwg_IDCert->item(m_idCertVector.size()-1)->setSizeHint(QSize(0, 160));  //每次改变Item的高度

        connect(certContent,SIGNAL(signalPassCert(bool,const QString&)),
                this,SIGNAL(signalPassCert(bool,const QString&)));
    }
    else
    {
        errorShow(msg);
        emit signalRefrushCertList(1);  /// 获取酒店审核的列表
    }
}

void CertificateCenter::slotUserCertResult(bool result, const QString& msg)
{
    if (result)
    {
        emit signalRefrushCertList(getCurrentIndex());
    }
    else
    {
         errorShow(msg);
    }
}

void CertificateCenter::errorShow(const QString& msg)
{
    QMessageBox::warning(this, "警告", msg, QMessageBox::Yes, QMessageBox::Yes);
}

void CertificateCenter::cleanUserCertList()
{
    while(ui->lwg_IDCert->count() > 0)
    {
        QListWidgetItem * item = ui->lwg_IDCert->takeItem(0);
        delete item;
    }

    ui->lb_IDCertNum->setText("0");
    m_idCertVector.clear();
}

void CertificateCenter::slotSetUserCertImage(bool result, const QString& msg, const QImage&image)
{
    if (result)
    {
        for (int i = 0; i < m_idCertVector.size(); i++)
        {
            if (msg == m_idCertVector[i]->getUserID())
            {
                m_idCertVector[i]->setUserImage(image);
                break;
            }
        }
    }
    else
    {
        qDebug() << "CommentManage::slotSetCommentImage" << msg;
        emit signalRefrushCertList(1);  /// 获取酒店审核的列表
    }
}
